package com.dlxj.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.dlxj.dao.BaseDAO;
import com.dlxj.entity.Group;
import com.dlxj.entity.LoginLog;
import com.dlxj.entity.Model;
import com.dlxj.entity.Purview;
import com.dlxj.entity.User;
import com.dlxj.service.PurviewService;
import com.dlxj.service.UserService;
import com.dlxj.service.WaiTransactService;
import com.dlxj.util.ConstClass;
import com.dlxj.util.PageBean;
import com.dlxj.util.StringUtil;

public class UserServiceImpl implements UserService {

	PurviewService purviewService;
	WaiTransactService waiTransactService;
	BaseDAO baseDAO;

	@Override
	public int login(User user) {

		List<User> users = (List<User>) baseDAO.findByHql(
				"from User where username =? and password=?", new Object[] {
						user.getUsername(), user.getPassword() });
		user = null;
		if (users.size() > 0) {
			User u = users.get(0);
			if (u.getState() == ConstClass.ENABLE) {
				LoginLog log = new LoginLog();
				log.setUid(u.getId());
				log.setTime(new Date());
				log.setIp(ServletActionContext.getRequest().getRemoteAddr());
				List<LoginLog> loginLogs = (List<LoginLog>) baseDAO
						.findByHql("from LoginLog where uid = " + u.getId()
								+ " order by time desc");
				if (loginLogs.size() > 0) {
					u.setLastLoginDate(StringUtil.date2str(loginLogs.get(0)
							.getTime(), "yyyy-MM-dd HH:mm:ss"));
					u.setLastLoginIp(loginLogs.get(0).getIp());
				}
				baseDAO.save(log);

				User suser = users.get(0);
				ServletActionContext.getRequest().getSession()
						.setAttribute("user", suser);
				int count = waiTransactService.findWaiTransactList(1, 9999)
						.getTotal();
				suser.setWaitCount(count);
				return ConstClass.LOGIN_SUCCESS;
			}
			return ConstClass.LOGIN_DENIED;
		}

		return ConstClass.LOGIN_ERROR;
	}

	@Override
	public void loginOut() {
		ServletActionContext.getRequest().getSession()
				.setAttribute("user", null);
	}

	@Override
	public int modifyInfo(User user) {
		User u = (User) baseDAO.findById(User.class, user.getId());
		u.setAge(user.getAge());
		u.setEmail(user.getEmail());
		u.setName(user.getName());
		u.setPhone(user.getPhone());
		baseDAO.update(u);
		User sessionUser = (User) ServletActionContext.getRequest()
				.getSession().getAttribute("user");
		sessionUser.setAge(user.getAge());
		sessionUser.setEmail(user.getEmail());
		sessionUser.setName(user.getName());
		sessionUser.setPhone(user.getPhone());
		ServletActionContext.getRequest().getSession()
				.setAttribute("user", sessionUser);
		return ConstClass.UPDATE_SUCCESS;
	}

	@Override
	public PageBean findUserList(String name, int pageIndex) {
		if (name == null || name.trim().equals("")) {
			return baseDAO.findPageBeanHQL("from User", pageIndex, 10, null);
		}
		return baseDAO.findPageBeanHQL("from User where name like ?",
				pageIndex, 10, new Object[] { "%" + name + "%" });
	}

	@Override
	public int modifyState(int uid, int state) {
		User user = (User) baseDAO.findById(User.class, uid);
		user.setState(state);
		baseDAO.update(user);
		return ConstClass.UPDATE_SUCCESS;
	}

	@Override
	public int addUser(User user) {
		baseDAO.save(user);
		return ConstClass.ADD_SUCCESS;
	}

	@Override
	public int modifyLeaveDate(int uid, Date date) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteUser(int uid) {
		baseDAO.delete(User.class, uid);
		return ConstClass.DELETE_SUCCESS;
	}

	@Override
	public List<Model> findModel(int uid) {

		User user = (User) baseDAO.findById(User.class, uid);
		if (user != null) {

			int pid = user.getGroup().getPurview().getId();

			List<Model> groupModels = purviewService.findModelByPid(pid, true);
			Purview userPurview = user.getPurview();
			if (userPurview != null) {
				List<Model> userModels = purviewService.findModelByPid(
						userPurview.getId(), true);
				for (Model model : userModels) {
					groupModels.add(model);
				}
			}
			return groupModels;
		}
		return null;
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public PurviewService getPurviewService() {
		return purviewService;
	}

	public void setPurviewService(PurviewService purviewService) {
		this.purviewService = purviewService;
	}

	@Override
	public int updatePassword(int uid, String oldpwd, String newpwd) {
		User u = (User) baseDAO.findById(User.class, uid);
		if (u.getPassword().equals(oldpwd)) {
			u.setPassword(newpwd);
			baseDAO.save(u);
			return ConstClass.UPDATE_SUCCESS;
		}
		return ConstClass.UPDATE_FAILURE;
	}

	@Override
	public List<User> findByGroupName(String name) {
		return (List<User>) baseDAO.findByHql("from User where group.name = ?",
				new Object[] { name });
	}

	@Override
	public User findById(int id) {
		return (User) baseDAO.findById(User.class, id);
	}

	public WaiTransactService getWaiTransactService() {
		return waiTransactService;
	}

	public void setWaiTransactService(WaiTransactService waiTransactService) {
		this.waiTransactService = waiTransactService;
	}

	@Override
	public int modifyGroup(int uid, Group group) {

		User user = (User) baseDAO.findById(User.class, uid);
		user.setGroup(group);
		baseDAO.update(user);
		return ConstClass.UPDATE_SUCCESS;
	}

	@Override
	public List<Group> findgroup() {
		
		return (List<Group>)baseDAO.findByHql("from Group ");
	}

}
